package com.kangtushizhe.controller;

import com.kangtushizhe.emuns.User;
import com.kangtushizhe.emuns.UserRole;
import com.kangtushizhe.entities.CommonResult;
import com.kangtushizhe.service.UserRoleService;
import com.kangtushizhe.service.UserService;
import com.kangtushizhe.utils.JwtUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author LiuZiMing
 * @since 2024-01-26
 */
@RestController
@RequestMapping("/user")
@Tag(name = "用户")
public class UserController {
    @Resource
    UserRoleService userRoleService;
    @Resource
    private UserService userService;
    @Resource
    private JwtUtil jwtUtil;

    //获取所有用户
    @GetMapping("/all")
    @Operation(summary = "获取所有用户", description = "获取所有用户")
    public CommonResult getAllCarts() {
        List<User> allCarts = userService.list();
        return new CommonResult(200, "获取所有用户成功", allCarts);
    }

    //创建新用户
    @PostMapping("/")
    @Operation(summary = "创建新用户", description = "创建新用户")
    @Parameters({
//            @Parameter(name = "id", description = "id", required = true),
            @Parameter(name = "name", description = "昵称", required = true),
            @Parameter(name = "username", description = "用户名", required = true),
            @Parameter(name = "password", description = "密码", required = true),
            @Parameter(name = "remark", description = "备注信息", required = true),
    })
    public CommonResult createCart(@RequestParam(required = true) String name,
                                   @RequestParam(required = true) String username,
                                   @RequestParam(required = true) String password,
                                   @RequestParam(required = true) String remark
    ) {
        User hasUser = userService.getUserByUsername(username);
        if (hasUser != null) {
            return new CommonResult(400, "请重设用户名");
        } else {
            boolean userCreateResult = userService.save(new User(null, name, username, password, remark));
            User user = userService.getUserByUsername(username);
            Integer userId = user.getId();
            boolean userRoleCreateResult = userRoleService.save(new UserRole(userId, 3));
            if (userCreateResult && userRoleCreateResult) {
                return new CommonResult(200, "创建用户成功,请登录");
            } else {
                return new CommonResult(400, "创建用户失败,请重试");
            }
        }

    }

    //根据id更新用户信息
    @PutMapping("/id")
    @Operation(summary = "更新用户", description = "更新用户")
    @Parameters({
            @Parameter(name = "id", description = "id", required = true),
            @Parameter(name = "name", description = "昵称", required = false),
            @Parameter(name = "username", description = "用户名", required = false),
            @Parameter(name = "password", description = "密码", required = false),
            @Parameter(name = "remark", description = "备注信息", required = false),
    })
    public CommonResult getCartById(@RequestParam(required = true) Integer id,
                                    @RequestParam(required = false) String name,
                                    @RequestParam(required = false) String username,
                                    @RequestParam(required = false) String password,
                                    @RequestParam(required = false) String remark,
                                    HttpServletRequest request
    ) {
        User user = userService.getById(id);
        if (user != null) {
            User hasUser = userService.getUserByUsername(username);
            if (hasUser != null) {
                return new CommonResult(400, "请重设用户名");
            } else {
                boolean updateResult = userService.updateById(new User(id, name, username, password, remark));
                if (updateResult) {
                    return new CommonResult(200, "更新用户信息成功");
                } else {
                    return new CommonResult(400, "更新用户信息失败,请重试");
                }
            }
        } else {
            return new CommonResult(404, "根据令牌未找到用户");
        }
    }

    //根据id获取用户信息
    @GetMapping("/id")
    @Operation(summary = "获取用户", description = "() => 获取用户")
    @Parameters({
            @Parameter(name = "id", description = "id", required = true),
    })
    public CommonResult updateCartById(@RequestParam(required = true) Integer id,
                                       HttpServletRequest request) {
        User user = userService.getById(id);
        if (user != null) {
            return new CommonResult(200, "根据令牌获取用户成功", user);
        } else {
            return new CommonResult(404, "根据令牌未找到用户");
        }
    }

    //根据id移除用户信息
    @DeleteMapping("/")
    @Operation(summary = "移除用户", description = "() => 移除用户")
    @Parameters({
            @Parameter(name = "id", description = "id", required = true)
    })
    public CommonResult removeCartById(@RequestParam(required = true) Integer id,
                                       HttpServletRequest request) {
        User user = userService.getById(id);
        if (user != null) {
            boolean removeResult = userService.removeById(id);
            if (removeResult) {
                return new CommonResult(200, "删除用户成功", user);
            } else {
                return new CommonResult(200, "删除用户失败,请重试", user);
            }
        } else {
            return new CommonResult(404, "根据令牌未找到用户");
        }
    }
}
